<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

class TaobaoOrder extends Model
{
    use HasFactory;

    protected $table = 'taobao_orders';
    protected $primaryKey = 'trade_id';
    public $incrementing = false;
    protected $keyType = 'string';

    protected $fillable = [
        'trade_id',
        'trade_parent_id',
        'item_id',
        'item_title',
        'item_price',
        'item_num',
        'click_time',
        'tb_paid_time',
        'tk_create_time',
        'tk_paid_time',
        'modified_time',
        'alipay_total_price',
        'pub_share_fee',
        'pub_share_pre_fee',
        'total_commission_fee',
        'income_rate',
        'pub_share_rate',
        'tk_total_rate',
        'total_commission_rate',
        'seller_nick',
        'seller_shop_title',
        'adzone_id',
        'adzone_name',
        'site_id',
        'site_name',
        'pub_id',
        'item_img',
        'item_link',
        'item_category_name',
        'tk_status',
        'refund_tag',
        'order_type',
        'terminal_type',
        'tk_order_role',
        'flow_source',
        'marketing_type',
        'subsidy_type',
        'subsidy_fee',
        'subsidy_rate',
    ];

    protected $casts = [
        'item_price' => 'decimal:2',
        'alipay_total_price' => 'decimal:2',
        'pub_share_fee' => 'decimal:2',
        'pub_share_pre_fee' => 'decimal:2',
        'total_commission_fee' => 'decimal:2',
        'income_rate' => 'decimal:2',
        'pub_share_rate' => 'decimal:2',
        'tk_total_rate' => 'decimal:2',
        'total_commission_rate' => 'decimal:2',
        'subsidy_fee' => 'decimal:2',
        'subsidy_rate' => 'decimal:2',
        'click_time' => 'datetime',
        'tb_paid_time' => 'datetime',
        'tk_create_time' => 'datetime',
        'tk_paid_time' => 'datetime',
        'modified_time' => 'datetime',
    ];

    // 状态常量
    const STATUS_PAID = 12;        // 付款
    const STATUS_CLOSED = 13;       // 关闭
    const STATUS_RECEIVED = 14;     // 确认收货
    const STATUS_SETTLED = 3;       // 结算成功

    // 获取状态文本
    public function getStatusTextAttribute()
    {
        return match ($this->tk_status) {
            self::STATUS_PAID => '已付款',
            self::STATUS_CLOSED => '已关闭',
            self::STATUS_RECEIVED => '已收货',
            self::STATUS_SETTLED => '结算成功',
            default => '未知状态',
        };
    }
}
